;(function ($, window, undefined) {
    'use strict';

    var isMobile = {
        Android: function () {
            return navigator.userAgent.match(/Android/i);
        },
        BlackBerry: function () {
            return navigator.userAgent.match(/BlackBerry/i);
        },
        iOS: function () {
            return navigator.userAgent.match(/iPhone|iPad|iPod/i);
        },
        Opera: function () {
            return navigator.userAgent.match(/Opera Mini/i);
        },
        Windows: function () {
            return navigator.userAgent.match(/IEMobile/i);
        },
        any: function () {
            return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
        }
    };

    // timeago.js
    $("abbr.timeago").timeago();

    $.widget("custom.catcomplete", $.ui.autocomplete, {
        _renderMenu: function (ul, items) {
            var that = this,
                currentCategory = "";
            $.each(items, function (index, item) {
                if (item.Category != currentCategory) {
                    ul.append("<li class='ui-autocomplete-category'>" + item.Category + "</li>");
                    currentCategory = item.Category;
                }
                that._renderItem(ul, item);
            });

            console.log(ul);
            //ul.append("<li class='ui-autocomplete-category'><a href='#search'>" + "Advanced Search" + "</a></li>");
        },
        _renderItem: function (ul, item) {
            var x = "<a href='" + item.Link + "'>" + item.Image + " <span class='text'>" + item.Text + "</span><span class='subtext'>" + item.SubText + "</span></a>";
            return $("<li></li>")
                .data("item.autocomplete", item)
                .append(x)
                .appendTo(ul);
        }
    });

    var cache = {}, lastXhr;
    $("#omnisearch").catcomplete({
        select: function (ul, data) {
            window.location = data.item.Link;
        },
        source: function (request, response) {
            var term = request.term;
            if (term in cache) {
                response(cache[term]);
                return;
            }

            lastXhr = $.getJSON('/Search', request, function (data, status, xhr) {
                cache[term] = data;
                if (xhr === lastXhr) {
                    response(data);
                }
            });
        }
    });

    var $doc = $(document),
        Modernizr = window.Modernizr;

  
    $.fn.foundationAlerts ? $doc.foundationAlerts() : null;
    $.fn.foundationAccordion ? $doc.foundationAccordion() : null;

    //$.fn.foundationTooltips ? $doc.foundationTooltips() : null;

    $('input, textarea').placeholder();
  
  
    $.fn.foundationButtons          ? $doc.foundationButtons() : null;
  
  
    $.fn.foundationNavigation       ? $doc.foundationNavigation() : null;
  
  
    $.fn.foundationTopBar           ? $doc.foundationTopBar({breakPoint: 940}) : null;
  
  
    $.fn.foundationMediaQueryViewer ? $doc.foundationMediaQueryViewer() : null;
  
    
    $.fn.foundationTabs             ? $doc.foundationTabs() : null;
    
    $("#featured").orbit();
  

  // UNCOMMENT THE LINE YOU WANT BELOW IF YOU WANT IE8 SUPPORT AND ARE USING .block-grids
  // $('.block-grid.two-up>li:nth-child(2n+1)').css({clear: 'both'});
  // $('.block-grid.three-up>li:nth-child(3n+1)').css({clear: 'both'});
  // $('.block-grid.four-up>li:nth-child(4n+1)').css({clear: 'both'});
  // $('.block-grid.five-up>li:nth-child(5n+1)').css({clear: 'both'});

  // Hide address bar on mobile devices
    if (Modernizr.touch) {
    $(window).load(function () {
        setTimeout(function () {
        window.scrollTo(0, 1);
        }, 0);
    });
    }

})(jQuery, this);
